package com.shrct.businessprocess.customerservice.impl;

import com.shrct.businessprocess.customerservice.RCRequireStatus;
import com.shrct.businessprocess.customerservice.RCRoutineService;
import com.shrct.businessprocess.customerservice.RCRoutineServiceDAO;
import com.shrct.businessprocess.customerservice.RCRoutineServiceInfo;
import com.shrct.businessprocess.customerservice.RCRoutineServiceInfoStatus;
import com.shrct.businessprocess.customerservice.RCServiceContractDelegate;
import com.shrct.businessprocess.customerservice.proxy.RCRoutineServiceInfoProxy;
import edu.fudan.langlab.domain.statemachine.ModelObjectLogService;
import edu.fudan.mylang.pf.BaseModelObject;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.pf.IObjectFactory;
import java.util.Collection;
import java.util.Date;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Table;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.hibernate.annotations.DiscriminatorOptions;
import org.springframework.beans.factory.BeanFactory;

@Entity
@DiscriminatorValue("RCROUTINESERVICEINFO_TYPE")
@DiscriminatorOptions(force = true)
@DiscriminatorColumn(length = 64)
@Table(name = "rcroutineserviceinfo")
public class RCRoutineServiceInfoImpl extends BaseModelObject implements RCRoutineServiceInfo {
  public RCRoutineServiceInfoImpl() {
    super();
  }
  
  private RCRoutineServiceInfoStatus Status;
  
  public RCRoutineServiceInfoStatus getStatus() {
    return this.Status;
  }
  
  private ModelObjectLogService getModelObjectLogService() {
    return getBeanFactory().getBean(ModelObjectLogService.class);
  }
  
  private void _start() {
    
    this.requireRoutineService = RCRequireStatus.unknown;
    Status=RCRoutineServiceInfoStatus.waiting4Initialize;
  }
  
  public void start() {
    if(Status==null){
    	_start();
    	getModelObjectLogService().createLog(this,"Status","Start");
    	getBeanFactory().getBean(com.shrct.businessprocess.customerservice.events.RCRoutineServiceInfoEventsManager.class).fireStarted(this);					
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException("allready started");
    }
  }
  
  private void _setAsNotRequired() {
    
    this.requireRoutineService = RCRequireStatus.notRequired;
    Status=RCRoutineServiceInfoStatus.notRequired;
  }
  
  public void setAsNotRequired() {
    if(Status==RCRoutineServiceInfoStatus.waiting4Initialize){
    	_setAsNotRequired();
    	getModelObjectLogService().createLog(this,"Status","setAsNotRequired");
    	getBeanFactory().getBean(com.shrct.businessprocess.customerservice.events.RCRoutineServiceInfoEventsManager.class).fireSetAsNotRequired(this);						
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException(Status.name());
    }
  }
  
  private void _setAsRequired() {
    
    this.requireRoutineService = RCRequireStatus.required;
    Status=RCRoutineServiceInfoStatus.ready4Schedule;
  }
  
  public void setAsRequired() {
    if(Status==RCRoutineServiceInfoStatus.waiting4Initialize||Status==RCRoutineServiceInfoStatus.notRequired){
    	_setAsRequired();
    	getModelObjectLogService().createLog(this,"Status","setAsRequired");
    	getBeanFactory().getBean(com.shrct.businessprocess.customerservice.events.RCRoutineServiceInfoEventsManager.class).fireSetAsRequired(this);						
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException(Status.name());
    }
  }
  
  private void _cancel() {
    
    IGenericQuery<RCServiceContractDelegate> query=getObjectFactory().createGenericQuery(RCServiceContractDelegate.class,"select e1 from com.shrct.businessprocess.customerservice.impl.RCServiceContractDelegateImpl as e1   where ((e1.routineServiceInfo) = (:this)) and  e1.active = 1      ").setParameter("this",this);
    final RCServiceContractDelegate serviceContract = query.uniqueResult();
    BeanFactory _beanFactory = this.getBeanFactory();
    final RCRoutineServiceDAO routineServiceDAO = _beanFactory.<RCRoutineServiceDAO>getBean(RCRoutineServiceDAO.class);
    IGenericQuery<RCRoutineService> query_1=getObjectFactory().createGenericQuery(RCRoutineService.class,"select e1 from com.shrct.businessprocess.customerservice.impl.RCRoutineServiceImpl as e1   where ((e1.projectInfo) = (:serviceContract)) and  e1.active = 1      ").setParameter("serviceContract",serviceContract);
    Collection<RCRoutineService> _list = query_1.list();
    final Procedure1<RCRoutineService> _function = new Procedure1<RCRoutineService>() {
        public void apply(final RCRoutineService it) {
          routineServiceDAO.delete(it);
        }
      };
    IterableExtensions.<RCRoutineService>forEach(_list, _function);
    this.requireRoutineService = RCRequireStatus.unknown;
    Status=RCRoutineServiceInfoStatus.waiting4Initialize;
  }
  
  public void cancel() {
    if(Status==RCRoutineServiceInfoStatus.notRequired||Status==RCRoutineServiceInfoStatus.ready4Schedule){
    	_cancel();
    	getModelObjectLogService().createLog(this,"Status","cancel");
    	getBeanFactory().getBean(com.shrct.businessprocess.customerservice.events.RCRoutineServiceInfoEventsManager.class).fireCancel(this);						
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException(Status.name());
    }
  }
  
  private void _updateInfo(final Procedure1<? super RCRoutineServiceInfo> preInit) {
    
    preInit.apply(this);
  }
  
  public void updateInfo(final Procedure1<? super RCRoutineServiceInfo> preInit) {
    if(Status==RCRoutineServiceInfoStatus.ready4Schedule||Status==RCRoutineServiceInfoStatus.scheduled){
    	_updateInfo(preInit);
    	getModelObjectLogService().createLog(this,"Status","updateInfo");
    	getBeanFactory().getBean(com.shrct.businessprocess.customerservice.events.RCRoutineServiceInfoEventsManager.class).fireUpdateInfo(this,preInit);						
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException(Status.name());
    }
  }
  
  private void _schedule() {
    
    Status=RCRoutineServiceInfoStatus.scheduled;
  }
  
  public void schedule() {
    if(Status==RCRoutineServiceInfoStatus.ready4Schedule||Status==RCRoutineServiceInfoStatus.scheduled){
    	_schedule();
    	getModelObjectLogService().createLog(this,"Status","schedule");
    	getBeanFactory().getBean(com.shrct.businessprocess.customerservice.events.RCRoutineServiceInfoEventsManager.class).fireSchedule(this);						
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException(Status.name());
    }
  }
  
  public Boolean isWaiting4Initialize() {
    return com.shrct.businessprocess.customerservice.RCRoutineServiceInfoStatus.waiting4Initialize==Status;
  }
  
  public Boolean isNotRequired() {
    return com.shrct.businessprocess.customerservice.RCRoutineServiceInfoStatus.notRequired==Status;
  }
  
  public Boolean isReady4Schedule() {
    return com.shrct.businessprocess.customerservice.RCRoutineServiceInfoStatus.ready4Schedule==Status;
  }
  
  public Boolean isScheduled() {
    return com.shrct.businessprocess.customerservice.RCRoutineServiceInfoStatus.scheduled==Status;
  }
  
  private RCRequireStatus requireRoutineService = RCRequireStatus.unknown;
  
  public RCRequireStatus getRequireRoutineService() {
    return this.requireRoutineService;
  }
  
  public RCRoutineServiceInfo setRequireRoutineService(final RCRequireStatus requireRoutineService) {
    this.requireRoutineService = requireRoutineService;
    return this;			
    
  }
  
  private Date serviceStart;
  
  public Date getServiceStart() {
    return this.serviceStart;
  }
  
  public RCRoutineServiceInfo setServiceStart(final Date serviceStart) {
    this.serviceStart = serviceStart;
    return this;			
    
  }
  
  private Date serviceEnd;
  
  public Date getServiceEnd() {
    return this.serviceEnd;
  }
  
  public RCRoutineServiceInfo setServiceEnd(final Date serviceEnd) {
    this.serviceEnd = serviceEnd;
    return this;			
    
  }
  
  private Integer servicePeriod;
  
  public Integer getServicePeriod() {
    return this.servicePeriod;
  }
  
  public RCRoutineServiceInfo setServicePeriod(final Integer servicePeriod) {
    this.servicePeriod = servicePeriod;
    return this;			
    
  }
  
  private String comment;
  
  public String getComment() {
    return this.comment;
  }
  
  public RCRoutineServiceInfo setComment(final String comment) {
    this.comment = comment;
    return this;			
    
  }
  
  public static RCRoutineServiceInfo create(final IObjectFactory objectFactory) {
    com.shrct.businessprocess.customerservice.RCRoutineServiceInfo rCRoutineServiceInfo = new com.shrct.businessprocess.customerservice.impl.RCRoutineServiceInfoImpl(
    );
    objectFactory.create(rCRoutineServiceInfo);
    return rCRoutineServiceInfo;			
    
  }
  
  public static RCRoutineServiceInfo createRCRoutineServiceInfoImpl(final IObjectFactory objectFactory, final Procedure1<RCRoutineServiceInfo> init) {
    com.shrct.businessprocess.customerservice.RCRoutineServiceInfo rCRoutineServiceInfo = new com.shrct.businessprocess.customerservice.impl.RCRoutineServiceInfoImpl(
    );
    objectFactory.create(rCRoutineServiceInfo);
    init.apply(rCRoutineServiceInfo);
    
    return rCRoutineServiceInfo;			
    
  }
  
  public RCRoutineServiceInfoProxy toProxy() {
    com.shrct.businessprocess.customerservice.proxy.RCRoutineServiceInfoProxy proxy = new com.shrct.businessprocess.customerservice.proxy.RCRoutineServiceInfoProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    proxy.setStatus(this.getStatus().name());
    return proxy;
    
  }
}
